﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace SystemX.Web
{
    public static partial class ObjectX
    {
        /// <summary>
        /// Convert data a to an object using javascript serializer
        /// </summary>
        /// <param name="jsonStr">string data like "{xx:'1',yy:'2'}"</param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T DeserializeJSONStr<T>(this string jsonStr)
        {
            try
            {
                JavaScriptSerializer jss = new JavaScriptSerializer();
                return jss.Deserialize<T>(jsonStr.Trim());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Serializer a strong typed object to json string using JavaScriptSerializer
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToJSONStr<T>(this T obj)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            return jss.Serialize(obj);
        }
    }
}
